/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.QuyDinhDTO;
import DTO.TuyenTauDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

public class QuyDinhDAO {
public static boolean Them(QuyDinhDTO n) {
        try {
            Connection conn = DBConnect.Connect();
            Statement statement = conn.createStatement();
            String sql = String.format ("INSERT INTO QUY_DINH (ID, GHI_CHU, GIA_TRI, DON_VI_TINH,NGAY_TAO) "
                    + "values ('%s','%s','%f','%s','%s')",n.iD, n.ghiChu, n.giaTri, n.donViTinh,n.ngayTao);
            statement.executeUpdate(sql);
            conn.close();
            return true;
           // JOptionPane.showMessageDialog(null, "Thêm thành công!!!", "Thông báo", 1);
        } catch (Exception ex) {
           // JOptionPane.showMessageDialog(null, "Thêm Không thành công!!!", "Thông báo", 1);
        }
        return false;
    }

    public static boolean Xoa(String id) {
        try {
            Connection conn = DBConnect.Connect();
            String strSQL = "delete from quy_dinh where id = ?";
            PreparedStatement pst = conn.prepareStatement(strSQL);
            pst.setString(1, id);
            pst.executeUpdate();
            //JOptionPane.showMessageDialog(null, "Xóa thành công!!!", "Thông báo", 1);
            pst.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
           // JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
    
    public static ResultSet TimKiemTatCa() {
        try {
            Connection conn = DBConnect.Connect();
            String sql = "Select * from quy_dinh";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
          //  conn.close();
            return rs;

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
            return null;
        }

    }
      public static boolean KiemTraTonTai(String maquydinh)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Select * From quy_dinh Where id like ?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, maquydinh);
            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next())
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
       public static boolean Update(QuyDinhDTO C, String id)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "update QUY_DINH set  GHI_CHU = ?, GIA_TRI = ?, DON_VI_TINH = ?, NGAY_TAO =?  where ID = ? ";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, C.ghiChu);
            ps.setDouble(2, C.giaTri);
            ps.setString(3,C.donViTinh);
            ps.setString(4,C.ngayTao);

            ps.setString(5,id);
            int n = ps.executeUpdate();
            con.close();
            
            if (n == 1)
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
}